home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / lib / perl5 / Mouse / Role.pm < prev    next >
Encoding:
Perl POD Document  |  2010-07-26  |  4.5 KB  |  219 lines

  1. package Mouse::Role;
  2. use Mouse::Exporter; # enables strict and warnings
  3.  
  4. our $VERSION = '0.64';
  5.  
  6. use Carp         qw(confess);
  7. use Scalar::Util qw(blessed);
  8.  
  9. use Mouse::Util  qw(not_supported);
  10. use Mouse::Meta::Role;
  11. use Mouse ();
  12.  
  13. Mouse::Exporter->setup_import_methods(
  14.     as_is => [qw(
  15.         extends with
  16.         has
  17.         before after around
  18.         override super
  19.         augment  inner
  20.  
  21.         requires excludes
  22.     ),
  23.         \&Scalar::Util::blessed,
  24.         \&Carp::confess,
  25.     ],
  26. );
  27.  
  28.  
  29. sub extends  {
  30.     Carp::croak "Roles do not support 'extends'";
  31. }
  32.  
  33. sub with     {
  34.     my $meta = Mouse::Meta::Role->initialize(scalar caller);
  35.     Mouse::Util::apply_all_roles($meta->name, @_);
  36.     return;
  37. }
  38.  
  39. sub has {
  40.     my $meta = Mouse::Meta::Role->initialize(scalar caller);
  41.     my $name = shift;
  42.  
  43.     $meta->throw_error(q{Usage: has 'name' => ( key => value, ... )})
  44.         if @_ % 2; # odd number of arguments
  45.  
  46.     if(ref $name){ # has [qw(foo bar)] => (...)
  47.         for (@{$name}){
  48.             $meta->add_attribute($_ => @_);
  49.         }
  50.     }
  51.     else{ # has foo => (...)
  52.         $meta->add_attribute($name => @_);
  53.     }
  54.     return;
  55. }
  56.  
  57. sub before {
  58.     my $meta = Mouse::Meta::Role->initialize(scalar caller);
  59.     my $code = pop;
  60.     for my $name($meta->_collect_methods(@_)) {
  61.         $meta->add_before_method_modifier($name => $code);
  62.     }
  63.     return;
  64. }
  65.  
  66. sub after {
  67.     my $meta = Mouse::Meta::Role->initialize(scalar caller);
  68.     my $code = pop;
  69.     for my $name($meta->_collect_methods(@_)) {
  70.         $meta->add_after_method_modifier($name => $code);
  71.     }
  72.     return;
  73. }
  74.  
  75. sub around {
  76.     my $meta = Mouse::Meta::Role->initialize(scalar caller);
  77.     my $code = pop;
  78.     for my $name($meta->_collect_methods(@_)) {
  79.         $meta->add_around_method_modifier($name => $code);
  80.     }
  81.     return;
  82. }
  83.  
  84.  
  85. sub super {
  86.     return if !defined $Mouse::SUPER_BODY;
  87.     $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
  88. }
  89.  
  90. sub override {
  91.     # my($name, $code) = @_;
  92.     Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_);
  93.     return;
  94. }
  95.  
  96. # We keep the same errors messages as Moose::Role emits, here.
  97. sub inner {
  98.     Carp::croak "Roles cannot support 'inner'";
  99. }
  100.  
  101. sub augment {
  102.     Carp::croak "Roles cannot support 'augment'";
  103. }
  104.  
  105. sub requires {
  106.     my $meta = Mouse::Meta::Role->initialize(scalar caller);
  107.     $meta->throw_error("Must specify at least one method") unless @_;
  108.     $meta->add_required_methods(@_);
  109.     return;
  110. }
  111.  
  112. sub excludes {
  113.     not_supported;
  114. }
  115.  
  116. sub init_meta{
  117.     shift;
  118.     my %args = @_;
  119.  
  120.     my $class = $args{for_class}
  121.         or Carp::confess("Cannot call init_meta without specifying a for_class");
  122.  
  123.     my $metaclass  = $args{metaclass}  || 'Mouse::Meta::Role';
  124.  
  125.     my $meta = $metaclass->initialize($class);
  126.  
  127.     $meta->add_method(meta => sub{
  128.         $metaclass->initialize(ref($_[0]) || $_[0]);
  129.     });
  130.  
  131.     # make a role type for each Mouse role
  132.     Mouse::Util::TypeConstraints::role_type($class)
  133.         unless Mouse::Util::TypeConstraints::find_type_constraint($class);
  134.  
  135.     return $meta;
  136. }
  137.  
  138. 1;
  139.  
  140. __END__
  141.  
  142. =head1 NAME
  143.  
  144. Mouse::Role - The Mouse Role
  145.  
  146. =head1 VERSION
  147.  
  148. This document describes Mouse version 0.64
  149.  
  150. =head1 SYNOPSIS
  151.  
  152.     package MyRole;
  153.     use Mouse::Role;
  154.  
  155. =head1 KEYWORDS
  156.  
  157. =head2 C<< meta -> Mouse::Meta::Role >>
  158.  
  159. Returns this role's metaclass instance.
  160.  
  161. =head2 C<< before (method|methods|regexp) -> CodeRef >>
  162.  
  163. Sets up a B<before> method modifier. See L<Moose/before>.
  164.  
  165. =head2 C<< after (method|methods|regexp) => CodeRef >>
  166.  
  167. Sets up an B<after> method modifier. See L<Moose/after>.
  168.  
  169. =head2 C<< around (method|methods|regexp) => CodeRef >>
  170.  
  171. Sets up an B<around> method modifier. See L<Moose/around>.
  172.  
  173. =head2 C<super>
  174.  
  175. Sets up the B<super> keyword. See L<Moose/super>.
  176.  
  177. =head2  C<< override method => CodeRef >>
  178.  
  179. Sets up an B<override> method modifier. See L<Moose/Role/override>.
  180.  
  181. =head2 C<inner>
  182.  
  183. This is not supported in roles and emits an error. See L<Moose/Role>.
  184.  
  185. =head2 C<< augment method => CodeRef >>
  186.  
  187. This is not supported in roles and emits an error. See L<Moose/Role>.
  188.  
  189. =head2 C<< has (name|names) => parameters >>
  190.  
  191. Sets up an attribute (or if passed an arrayref of names, multiple attributes) to
  192. this role. See L<Mouse/has>.
  193.  
  194. =head2 C<< confess(error) -> BOOM >>
  195.  
  196. L<Carp/confess> for your convenience.
  197.  
  198. =head2 C<< blessed(value) -> ClassName | undef >>
  199.  
  200. L<Scalar::Util/blessed> for your convenience.
  201.  
  202. =head1 MISC
  203.  
  204. =head2 import
  205.  
  206. Importing Mouse::Role will give you sugar.
  207.  
  208. =head2 unimport
  209.  
  210. Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the
  211. keywords (such as L</has>) it will break loudly instead breaking subtly.
  212.  
  213. =head1 SEE ALSO
  214.  
  215. L<Moose::Role>
  216.  
  217. =cut
  218.  
  219.